Skip to content

GH-20: Expose bw, cr, sf controls and CRC checks #21

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Mar 21, 2019

Conversation

applio
Copy link
Contributor

@applio applio commented Mar 15, 2019

This patch proposes the following enhancements without breaking existing user code or any default behaviors:

  • Add to RFM9x.__init__ a keyword input parameter, signal_bandwidth
  • Add to RFM9x.__init__ a keyword input parameter, coding_rate
  • Add to RFM9x.__init__ a keyword input parameter, spreading_factor
  • Add to RFM9x.__init__ a keyword input parameter, enable_crc
    The defaults remain a match for RadioHead Bw125Cr45Sf128 mode with CRC checking off.

This patch has been used successfully with Adafruit RFM95W. It has also successfully enabled communications between the Adafruit RFM95W and the TTGO LORA32 (an ESP32 board with a different RFM95 radio) at a variety of different settings, having verified that it really was communicating at those different bandwidths, etc.

This code was made possible through careful study of the code used in https://github.com/MZachmann/LightLora_Arduino to expose these same features. Credit goes to Mark Zachmann for having done the truly hard work of figuring out exactly how to tickle the registers in the hardware.

The adafruit/Adafruit_CircuitPython_RFM9x library is an excellent library and it feels intuitive when using it. Thank you for creating it and sharing it with others. I hope these enhancements can expand its reach.

This suggested patch was mentioned in issue GH-20.

Notably, this patch includes changes made in another pull request (GH-19).

@ladyada
Copy link
Member

ladyada commented Mar 15, 2019

hiya! thanks - would some of these would be better as 'property' types, rather than init arguments. that way people can adjust the factors depending on use cases?

@applio
Copy link
Contributor Author

applio commented Mar 15, 2019

I'd also thought of exposing them as properties though what I've seen in practice is that attempting to modify values of some of these, say rssi, after using the radio can cause an indefinite hang. That behavior exists when using the current implementation's rssi property. At the moment, I am unsure of how to fix this for rssi and so was hesitant to add similar complications with more properties that can be changed immediately after instantiation but not after using the radio to send/receive. I sure like the idea though.

@applio
Copy link
Contributor Author

applio commented Mar 15, 2019

We could go ahead and move these input parameters to each be a property, if you think that would make for a better api... and then hope we can come back to figure out how to avoid the hang I described on the rssi property (and these new ones)?

@ladyada
Copy link
Member

ladyada commented Mar 15, 2019

i think lets do properties, at worst you can just set them before you begin the radio, and if we fix the bug you'll be able to adjust them whenever!

@applio
Copy link
Contributor Author

applio commented Mar 15, 2019

Cool -- I'll make the change and then update my code that uses it to test.

@applio
Copy link
Contributor Author

applio commented Mar 15, 2019

Apologies, earlier I said I'd modified rssi via its property but that was incorrect -- I was messing with frequency_mhz instead.

@applio
Copy link
Contributor Author

applio commented Mar 15, 2019

The changes to expose as property appear to be working happily with my existing (now updated) code.

Playing around in the interactive shell, it also appears to do what I hoped. Here, rfm9x = adafruit_rfm9x.RFM9x(...):

>>> rfm9x.coding_rate
5
>>> rfm9x.coding_rate = 7
>>> rfm9x.coding_rate
7
>>> rfm9x.coding_rate = 200  # Exceeds max allowed
>>> rfm9x.coding_rate
8
>>> rfm9x.coding_rate = -1  # Also outside allowed
>>> rfm9x.coding_rate
5
>>> rfm9x.signal_bandwidth
250000
>>> rfm9x.signal_bandwidth = 10000  # Does not match a supported value
>>> rfm9x.signal_bandwidth
10400
>>> rfm9x.signal_bandwidth = 10**6  # Exceeds max allowed
>>> rfm9x.signal_bandwidth
500000
>>> rfm9x.signal_bandwidth = -1  # Also outside allowed
>>> rfm9x.signal_bandwidth
7800
>>> rfm9x.spreading_factor
7
>>> rfm9x.spreading_factor = 11
>>> rfm9x.spreading_factor
11
>>> rfm9x.spreading_factor = -1  # Outside allowed
>>> rfm9x.spreading_factor
6
>>> rfm9x.spreading_factor = 100  # Outside allowed
>>> rfm9x.spreading_factor
12

Likewise, enable_crc appears to behave itself:

>>> rfm9x.enable_crc
False
>>> rfm9x.enable_crc = True
>>> rfm9x.enable_crc
True
>>> rfm9x.enable_crc = 0
>>> rfm9x.enable_crc
False
>>> rfm9x.enable_crc = 42
>>> rfm9x.enable_crc
True

I have yet to experiment with which settings can be safely altered after the radio has been used. The above was all done before transmitting anything. Hopefully the radio just needs to be put back into an appropriate mode to permit changing tx_power or frequency_mhz, etc. later.

@ladyada
Copy link
Member

ladyada commented Mar 16, 2019

i'm diggin' it!
@jerryneedell @brentru y'all use this library the most, wanna look?

@jerryneedell
Copy link
Contributor

Looks great. I will run some tests with it tomorrow.

Copy link
Contributor

@jerryneedell jerryneedell left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I ran some tests to verify that this PR "does no harm" and it seems to work fine with my existing code.
The changes look great. There are a lot of features to experiment with and test, but since it does not break any existing code, I am happy to see it merged so more folks can use it.
Thank your for submitting this!

BTW -- I did test both with CP (ItsyBity_M4 and RFM9x breakout) -- and on a Raspberry Pi with breakout and bonnet.

@ladyada
Copy link
Member

ladyada commented Mar 17, 2019

thanks! @brentru check this still works with your examples you've done for the RFM9x?

@brentru
Copy link
Member

brentru commented Mar 20, 2019

@ladyada Missed this tag - I'll test this against the RFM9x Bonnet Code, examples I've written for learn, and CircuitPython_TinyLoRa first-thing tomorrow when I have access to that hardware and review.

Copy link
Member

@brentru brentru left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@applio Looks good, approved! Changes are compatible and non-breaking when used with Adafruit_CircuitPython_TinyLoRa (tested a few different modes/settings to exercise it).

@brentru brentru merged commit 2dbb850 into adafruit:master Mar 21, 2019
@applio applio deleted the enh_expose_Bw_Cr_Sf_controls branch March 21, 2019 16:43
adafruit-adabot added a commit to adafruit/Adafruit_CircuitPython_Bundle that referenced this pull request Mar 21, 2019
Updating https://github.com/adafruit/Adafruit_CircuitPython_MLX90393 to 1.2.0 from 1.1.3:
  > Merge pull request adafruit/Adafruit_CircuitPython_MLX90393#6 from microbuilder/master
  > Separated raw and processed data
  > Merge pull request adafruit/Adafruit_CircuitPython_MLX90393#5 from microbuilder/master

Updating https://github.com/adafruit/Adafruit_CircuitPython_NeoPixel to 3.3.6 from 3.3.5:
  > Merge pull request adafruit/Adafruit_CircuitPython_NeoPixel#40 from cpforbes/cpf-list-fix

Updating https://github.com/adafruit/Adafruit_CircuitPython_PN532 to 2.0.4 from 2.0.3:
  > Merge pull request adafruit/Adafruit_CircuitPython_PN532#18 from jerryneedell/jerryn_ntag203
  > Merge pull request adafruit/Adafruit_CircuitPython_PN532#16 from Tasm-Devil/master

Updating https://github.com/adafruit/Adafruit_CircuitPython_RFM9x to 1.1.5 from 1.1.4:
  > Merge pull request adafruit/Adafruit_CircuitPython_RFM9x#21 from applio/enh_expose_Bw_Cr_Sf_controls
  > Merge pull request adafruit/Adafruit_CircuitPython_RFM9x#19 from applio/fix_broken_CoC_link_tweaks

Updating https://github.com/adafruit/Adafruit_CircuitPython_Bitmap_Font to 1.0.2 from 1.0.1:
  > Merge pull request adafruit/Adafruit_CircuitPython_Bitmap_Font#8 from tannewt/fontio

Updating https://github.com/adafruit/Adafruit_CircuitPython_HID to 3.3.3 from 3.3.2:
  > Merge pull request adafruit/Adafruit_CircuitPython_HID#31 from dhalbert/gamepad-struct-format-fix
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants